package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.mapper.NewsMapper;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements INewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private NewsContentMapper contentMapper;


    @Override
    public PageInfo<News> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<News> list=newsMapper.selectForList(qo);

        return new PageInfo<News>(list);
    }

    @Override
    public void saveOrUpdate(News news) {
        NewsContent content = news.getNewsContent();
        //判断状态是否是发布或者推荐,是就给发布时间
        if (news.getState().equals(News.STATE_RELEASE) || news.getState().equals(News.STATE_HOT) ) {
            news.setReleaseTime(new Date());
        }

        if (news.getId() != null) {
            newsMapper.updateByPrimaryKey(news);
            content.setId(news.getId());
            contentMapper.updateByPrimaryKey(content);
        }else{
            newsMapper.insert(news);
            content.setId(news.getId());
            contentMapper.insert(content);
        }
    }

    @Override
    public News selectContentByNewsId(Long id) {
        return newsMapper.selectByPrimaryKey(id);
    }
}
